//
//  ViewController.m
//  UI_19_数据请求model嵌套
//
//  Created by li cheng on 16/8/10.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import "FindCar.h"
#import "Result.h"
#import "Brandlist.h"
#import "Brand.h"
#import "CarBrandTableViewCell.h"
@interface ViewController ()<UITabBarDelegate,UITableViewDataSource>
@property (nonatomic, strong) FindCar *findCar;
@property (nonatomic, strong) NSMutableArray *brandlistArray;
@property (weak, nonatomic) IBOutlet UITableView *carBandTableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
    
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.findCar.result.brandlist[section].letter;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.findCar.result.brandlist.count;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.findCar.result.brandlist[section].list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CarBrandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    Brand *brand = self.findCar.result.brandlist[indexPath.section].list[indexPath.row];
    cell.carBrandLabel.text = brand.name;
    cell.carBrandImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:brand.imgurl]]];
    return cell;
}
- (void)handleData{
    NSString *path  = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    //
    self.findCar = [FindCar modelWithDictionary:dic];
    
        
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
